package com.tyl.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tyl.ResultEntity;
import com.tyl.entity.*;
import com.tyl.service.IAdminRoleService;
import com.tyl.service.IRoleResourceService;
import com.tyl.service.IRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 汤云龙
 * @since 2022-03-17
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    private IAdminRoleService adminRoleService;
    @Resource
    private IRoleService roleService;
    @Resource
    private IRoleResourceService iRoleResourceService;
    @PostMapping("/grantRidForUserId")
    public ResultEntity GiveUserRole(@RequestBody GiveUserRole giveUserRole){
        //给用户赋予角色
        //循环得到所有的角色信息
        for (Integer roleId:giveUserRole.getRoleIds()){
            AdminRole adminRole = new AdminRole();
            //给用户表附上当前要赋予角色的用户
            adminRole.setAdminId(giveUserRole.getUserId());
            //给角色附上当前角色的id
            adminRole.setRoleId(roleId);
            //插入当前要赋值的用户角色表
            adminRoleService.save(adminRole);
        }
        return ResultEntity.ok("插入成功");
    }
    @GetMapping("/selectAll")
    public ResultEntity selectAll(){
        //查询所有的角色
        List<AdminRole> list = adminRoleService.list();
        System.out.println(list);
        return ResultEntity.ok(list);
    }
    @PostMapping("/add")
    public ResultEntity add(@RequestBody Role role){
        //添加角色
       roleService.save(role);
       return ResultEntity.ok();
    }
    @DeleteMapping("/delete/{roleId}")
    public ResultEntity delete(@PathVariable("roleId") Integer id){
        //删除角色
        roleService.removeById(id);
        return ResultEntity.ok();
    }
    @GetMapping("/selectOne/{roleId}")
    public ResultEntity selectById(@PathVariable("roleId") Integer roleId){
        //根据id查询单个用户
        Role role = roleService.getById(roleId);
        return ResultEntity.ok(role);
    }
    @PostMapping("/grantResource")
    public ResultEntity GiveRoleRoleResource(@RequestBody GiveRoleRoleResource giveRoleRoleResource){
        //给角色赋予权限
        //循环得到所有的权限信息
        for (Integer roleResourceIds:giveRoleRoleResource.getResourceIds()){
            RoleResource roleResource = new RoleResource();
            //给角色表附上当前要赋予角色的用户
            roleResource.setRoleId(giveRoleRoleResource.getRoleId());
            //给角色附上当前角色的id
            roleResource.setResourceId(roleResourceIds);
            //插入当前要赋值的用户角色表
            iRoleResourceService.save(roleResource);
        }
        return ResultEntity.ok("插入成功");
    }
    @GetMapping("/selectResourcesByRoleId/{roleId}")
    public ResultEntity selectByRoleId(@PathVariable("roleId") Integer roleId){
        QueryWrapper<RoleResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id","roleId");
        List<RoleResource> resourceList = iRoleResourceService.list();
        System.out.println(resourceList);
        return ResultEntity.ok(resourceList);
    }
    @PutMapping("/update")
    public ResultEntity updateRole(@RequestBody Role role){
        //修改角色
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",role.getRoleId());
        roleService.update(role,wrapper);
        return ResultEntity.ok();
    }


}

